
import { plainToInstance } from "class-transformer";
import ResultHelper from "../routes/utils/ResultHelper";
import { MarkerPoiEntity } from "../entities/MarkerPoiEntity";
import MarkerPoiDao from "../dao/MarkerPoiDao";
import { ResponseCode } from "../routes/utils/ResponseHelper";


export default class MarkerPoiService {

  /* eslint-disable @typescript-eslint/no-explicit-any */
  static async add(body: any) {

    try {

      const Marker = plainToInstance(MarkerPoiEntity, body)
      const errs = await Marker.validateThie()
      if (errs.length != 0) {
        return ResultHelper.error(errs)
      }

      const resultExite = await MarkerPoiService.findOne(Marker.u_id, Marker.id)
      if (resultExite.code === ResponseCode.PASS) {
        return ResultHelper.error("已存在")
      }

      const result = await MarkerPoiDao.add(Marker)
      if (JSON.stringify(result) !== "{}") {

        result.photos = typeof result.photos === "string" ? JSON.parse(result.photos) : {}
        result.extdata = typeof result.extdata === "string" ? JSON.parse(result.extdata) : {}

        return ResultHelper.success(result, "添加成功")
      } else {
        return ResultHelper.success({}, "操作失败")
      }

    } catch ({ message }) {
      return ResultHelper.error(message)
    }

  }

  static async del(uid: number, id: string) {

    if (!uid) {
      return ResultHelper.error("没有用户标识")
    }

    if (!id) {
      return ResultHelper.error("没有marker标识")
    }

    try {
      const result = await MarkerPoiDao.del(uid, id)

      return result ? ResultHelper.success({}, "删除成功") : ResultHelper.error("删除失败")

    } catch ({ message }) {
      return ResultHelper.error(message)
    }
  }

  static async getNameByMId(m_id: string) {
    if (!m_id) {
      return ResultHelper.error("没有marker标识")
    }

    try {
      const result = await MarkerPoiDao.getNameByMId(m_id)

      return result ? ResultHelper.success(result) : ResultHelper.error("不存在")
    } catch ({ message }) {
      return ResultHelper.error(message)
    }
  }

  static async findByUid(uid: number) {
    if (!uid) {
      return ResultHelper.error("没有用户标识")
    }

    try {
      const result = await MarkerPoiDao.findByUid(uid);
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      result.map((item: any) => {
        item.photos = item.photos === "" ? {} : item.photos = JSON.parse(item.photos)
        item.extdata = item.extdata === "" ? {} : item.extdata = JSON.parse(item.extdata)
      })

      return ResultHelper.success(result)

    } catch ({ message }) {
      return ResultHelper.error(message)
    }
  }


  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async modify(body: any) {
    if (!body.u_id) {
      return ResultHelper.error("没有用户标识")
    }

    if (!body.m_id) {
      return ResultHelper.error("没有用户标识")
    }

    const Marker = plainToInstance(MarkerPoiEntity, body)
    const errs = await Marker.validateThie(true)

    if (errs.length > 0) {
      return ResultHelper.error(errs)
    }

    try {
      const result = await MarkerPoiDao.modify(Marker);



      return result ? ResultHelper.success({}, "修改成功") : ResultHelper.error("修改失败")
    } catch ({ message }) {
      return ResultHelper.error(message)
    }
  }

  static async findOne(uid: number, id: string) {
    if (!uid) {
      return ResultHelper.error("没有用户标识")
    }

    if (!id) {
      return ResultHelper.error("没有marker标识")
    }

    try {
      const result = await MarkerPoiDao.findOne(uid, id)

      return result ? ResultHelper.success(result) : ResultHelper.error("不存在")
    } catch ({ message }) {
      return ResultHelper.error(message)
    }
  }

}